<?php

namespace app\beautiful\controller\admin;

use app\beautiful\model\GiftbagModel;

use app\BaseController;

use app\common\logic\DataLogic;
use think\Model;


class Giftbag extends BaseController{

    protected $name='礼包';



    public function index()

    {

        //$status=$this->request->param('status/d',1);

        $where=[

            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]

        ];
        $gifbag = new GiftbagModel();

        $order=['update_time'=>'desc','add_time'=>'desc'];

        $field="giftbag_id,name,coupon_ids,add_time";

        $append=[];


        $return = DataLogic::getDataList($gifbag,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        $return['new_list'] = [];
        foreach($return['list'] as $giftbag){

            $data['giftbag_id'] = $giftbag['giftbag_id'] ;
            $data['name'] = $giftbag['name'] ;
            $data['coupon_ids'] = explode(',',$giftbag['coupon_ids']) ;
            $data['coupon_qty'] = count($data['coupon_ids']) ;
            $data['add_time'] = $giftbag['add_time'] ;
            $return['new_list'][] =  $data;
        }

        unset($return['list']);
        $return['list'] = $return['new_list'];
        unset($return['new_list']);


        return $this->success($return);

    }



    public function show()

    {

        $giftbag_id=$this->request->param('giftbag_id/d',0);

        if($giftbag_id==0){

            return $this->success([]);

        }

        if(null === $giftbag= GiftbagModel::find($giftbag_id)){

            return $this->error($this->name.'不存在');

        }

        if($giftbag->shop_id != $this->request->shopId){

            return $this->error($this->name.'不存在');

        }

        $detail=[

            'name'=>$giftbag->name,

            'giftbag_id'=>$giftbag->giftbag_id,

            'coupon_ids' => explode(',',$giftbag->coupon_ids),

            'add_time'=>$giftbag->add_time,

        ];

        return $this->success(['detail'=>$detail]);

    }



    public function save()

    {

        $giftbag_id=$this->request->param('giftbag_id/d',0);

        $name=$this->request->param('name');

        $coupon_id=$this->request->param('coupon_id/a',[]);

        if(empty($name) || mb_strlen($name)>40){

            return $this->error('名称必须填写且长度不能大于40');

        }


        //优惠券
        if(empty($coupon_id) ||count($coupon_id) < 1){

           return $this->error('请选择优惠券');

        }

        $coupon_ids = implode(',',$coupon_id);

        if($giftbag_id != 0){

            if(null === $giftbag = GiftbagModel::find($giftbag_id)){

                return $this->error($this->name.'不存在');

            }


            if($giftbag->shop_id != $this->request->shopId){

                return $this->error($this->name.'不存在');

            }


        }else{

            $giftbag = new GiftbagModel();

        }

        $giftbag->shop_id = $this->request->shopId;
        $giftbag->coupon_ids  = $coupon_ids;
        $giftbag->name=$name;
        $giftbag->update_time=$this->request->time();

        $giftbag->save();

        return $this->success('保存成功');

    }



    public function del()

    {

        $giftbag_id=$this->request->param('giftbag_id/d',0);

        if(null === $giftbag = GiftbagModel::find($giftbag_id)){

            return $this->error($this->name.'不存在');

        }


        if($giftbag->shop_id !=$this->request->shopId){

            return $this->error($this->name.'不存在');

        }

        $giftbag->delete();

        return $this->success('删除成功');

    }



}

